#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/7/21 10:28
# @Author  : Samge
import pymysql
from itkz.resources.db.base import BaseLogDb


class MysqlBase(BaseLogDb):

    def __init__(self, host, user, password, database):
        self.conn = pymysql.connect(host=host, user=user, password=password, database=database)
        self.cursor = self.conn.cursor()

    def ExecList(self, sql):
        self.conn.ping(reconnect=True)
        cur = self.cursor
        cur.execute(sql)
        columns = [col[0] for col in cur.description]
        return [dict(zip(columns, row)) for row in cur.fetchall()]

    # 添加、更新、删除
    def ExecNonQuery(self, sql):
        # 检查连接是否断开，如果断开就进行重连
        self.conn.ping(reconnect=True)
        cur = self.cursor
        cur.execute(sql)
        self.conn.commit()

